package com.imay.live.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.imay.base.fragment.BaseDialogFragment;
import com.imay.base.util.DateUtil;
import com.imay.live.activity.AboutIMayActivity;
import com.imay.live.helper.GiftHelper;
import com.imay.live.model.GiftModel;
import com.imay.live.proto.room.GiftRankInfo;
import com.imay.live.proto.room.RoomStatInfo;
import com.imay.base.util.SpannableStringUtils;
import com.imay.live.view.ColorClickableSpan;
import com.imay.live.R;
import com.imay.live.view.custom.RoomEndGiftView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jeaman on 2017/3/27.
 * 直播结束
 */

public class LiveEndFragment extends BaseDialogFragment implements View.OnClickListener {
    private final static String BUNDLE_NAME_ERROR = "error";
    private final static String BUNDLE_NAME_WATCHER = "watcher";
    private final static String BUNDLE_NAME_CHARM = "charm";
    private final static String BUNDLE_NAME_DURATION = "duration";
    private final static String BUNDLE_NAME_FANS = "fans";
    private final static String BUNDLE_NAME_DIFF = "diff";
    private final static String BUNDLE_NAME_GIFTS = "gifts";

    public static LiveEndFragment newInstance(RoomStatInfo roomStatInfo) {
        Bundle bundle = new Bundle();
        if (roomStatInfo == null) {
            bundle.putBoolean(BUNDLE_NAME_ERROR, true);
        } else {
            bundle.putLong(BUNDLE_NAME_WATCHER, roomStatInfo.total);
            bundle.putLong(BUNDLE_NAME_CHARM, roomStatInfo.meili);
            bundle.putLong(BUNDLE_NAME_DURATION, roomStatInfo.duration);
            bundle.putLong(BUNDLE_NAME_FANS, roomStatInfo.newFollow);
            bundle.putInt(BUNDLE_NAME_DIFF, roomStatInfo.diffValue);
            bundle.putParcelableArrayList(BUNDLE_NAME_GIFTS, roomStatInfo.gifts);
        }
        LiveEndFragment fragment = new LiveEndFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_live_end;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setCancelable(false);   //不允许关闭
        Bundle bundle = getArguments();
        boolean error = bundle.getBoolean(BUNDLE_NAME_ERROR);
        long watcher = bundle.getLong(BUNDLE_NAME_WATCHER);
        long charm = bundle.getLong(BUNDLE_NAME_CHARM);
        long duration = bundle.getLong(BUNDLE_NAME_DURATION);
        long fans = bundle.getLong(BUNDLE_NAME_FANS);
        int diffCharm = bundle.getInt(BUNDLE_NAME_DIFF);
        ArrayList<GiftRankInfo> giftList = bundle.getParcelableArrayList(BUNDLE_NAME_GIFTS);

        TextView durationTv = $(R.id.live_end_tv_time);
        TextView watcherTv = $(R.id.live_end_tv_watcher);
        TextView fansTv = $(R.id.live_end_tv_fans);

        watcherTv.setText(String.valueOf(watcher));
        durationTv.setText(DateUtil.getTimeBySecond(duration));
        fansTv.setText(String.valueOf(fans));

        if (error) {
            SpannableStringUtils.Builder builder = SpannableStringUtils.getBuilder(getString(R.string.live_room_end_error))
                    .append(getString(R.string.live_room_end_service)).setClickSpan(new ColorClickableSpan(getResources().getColor(R.color.text_ff0)) {
                        @Override
                        public void onClick(View widget) {
                            if (getActivity() != null) {
                                AboutIMayActivity.start(getActivity());
                            }
                        }
                    });
            $(R.id.live_end_ll_error).setVisibility(View.VISIBLE);
            ((TextView) $(R.id.live_end_tv_error)).setText(builder.create());
        } else {
           if (giftList != null && giftList.size() > 0) {
               $(R.id.live_end_ll_charm).setVisibility(View.VISIBLE);
               TextView charmTv = $(R.id.live_end_tv_charm);
               TextView diffCharmTv = $(R.id.live_end_tv_diff_charm);
               RoomEndGiftView giftView1 = $(R.id.live_end_cv_gift1);
               RoomEndGiftView giftView2 = $(R.id.live_end_cv_gift2);
               RoomEndGiftView giftView3 = $(R.id.live_end_cv_gift3);

               List<RoomEndGiftView> giftViewList = new ArrayList<>();
               giftViewList.add(giftView1);
               giftViewList.add(giftView2);
               giftViewList.add(giftView3);

               charmTv.setText(getString(R.string.live_room_end_charm, charm));
               if (diffCharm > 0) {
                   diffCharmTv.setText(getString(R.string.live_room_end_diff_charm, diffCharm));
               } else {
                   diffCharmTv.setVisibility(View.INVISIBLE);
               }
               for (int i = 0; i < giftList.size(); i++) {
                   GiftRankInfo giftRankInfo = giftList.get(i);
                   if (i < giftViewList.size()) {
                       RoomEndGiftView giftView = giftViewList.get(i);
                       giftView.setVisibility(View.VISIBLE);
                       GiftModel gift = GiftHelper.getInstance().getGift(giftRankInfo.giftId);
                       if (gift != null) {
                           giftView.setData(gift.getGiftPic(), gift.getGiftName(), gift.getMoney(), giftRankInfo.count);
                       }
                   }
               }
           } else { //没有礼物
               $(R.id.live_end_tv_no_gift).setVisibility(View.VISIBLE);
           }

        }

        $(R.id.live_end_btn_back).setOnClickListener(this);
    }

    @Override
    protected boolean isMatchParent() {
        return true;
    }

    @Override
    protected float getDimAmount() {
        return 0.8f;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.live_end_btn_back:
                if (getActivity() != null) {
                    getActivity().onBackPressed();
                }
                break;
        }
    }
}
